const app = getApp()
var WxParse = require('../../wxParse/wxParse.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    yunfei:0,
    msg1:''
  },
  onShow: function () {
    var pages = getCurrentPages();
    var currPage = pages[pages.length - 1];
    console.log("列表页传过来的值", currPage.data);//此处既是上一页面传递过来的值
    this.setData({
      deliveryId: currPage.data.dlid,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    that.setData({
      tg_or_ms :options.isPro
    })
    // isPro 秒杀3 团购4
    if (options.isPro == 3) {
      that.setData({
        ids: options.ids,
        number: options.number,
        money: options.money,
        totalNum: options.number,
        total_price: options.money,
        seckillId: options.seckillId
      })
      that.msRequest(options)
    } else if (options.isPro == 4) {
      that.setData({
        ids: options.ids,
        number: options.number,
        money: options.money,
        totalNum: options.number,
        total_price: options.money,
        seckillId: options.seckillId
      })
      that.tgRequest(options)
    }
  },

  //秒杀进入购物清单
  msRequest: function (options) {
    var that = this
    wx.getStorage({
      key: 'openid',
      success: function (res) {
        let openid = res.data

        wx.request({
          url: app.globalData.URL + '/app/seckill/validateMiaosha?openid=' + openid + '&productId=' + options.ids + '&seckillId=' + options.seckillId,
          success: function (res) {
            if (res.data.flag == true) {
              wx.request({
                url: app.globalData.URL + '/app/seckill/settlement?openid=' + openid + '&id=' + options.ids + '&num=' + options.number + '&seckillId=' + options.seckillId,
                success: function (res) {
                  console.log("seckill/settlement", res.data)
                  that.setData({
                  
                    name: res.data.delivery.name,
                    phone: res.data.delivery.telPhone,
                    address: res.data.delivery.address,
                    deliveryId: res.data.delivery.id,
                    productName: res.data.qhcPro.productName,
                    price: res.data.seckill.price,
                    picture: res.data.seckill.picture
                  })
                }
              })
            } else {
              wx.showModal({
                title: '',
                content: res.data.msg,
                success: function (res) {
                  if (res.cancel) {
                    wx.navigateBack({})
                  } else {
                    wx.navigateBack({})
                  }
                },
              })
            }
          }
        })
      },
    })
  },
  //团购进入购物清单
  tgRequest: function (options) {
    var that = this
    wx.getStorage({
      key: 'openid',
      success: function (res) {
        let openid = res.data

        wx.request({
          url: app.globalData.URL + '/app/seckill/validateMiaosha?openid=' + openid + '&productId=' + options.ids + '&seckillId=' + options.seckillId,
          success: function (res) {
            if (res.data.flag == true) {
              wx.request({
                url: app.globalData.URL + '/app/seckill/settlement2?openid=' + openid + '&id=' + options.ids + '&num=' + options.number + '&seckillId=' + options.seckillId,
                success: function (res) {
                  console.log("seckill/settlement2", res.data)
                  that.setData({
                    name: res.data.delivery.name,
                    phone: res.data.delivery.telPhone,
                    address: res.data.delivery.address,
                    deliveryId: res.data.delivery.id,
                    productName: res.data.qhcPro.productName,
                    price: res.data.seckill.price,
                    picture: res.data.seckill.picture
                  })
                }
              })
            } else {
              wx.showModal({
                title: '',
                content: res.data.msg,
                success: function (res) {
                  if (res.cancel) {
                    wx.navigateBack({})
                  } else {
                    wx.navigateBack({})
                  }
                },
              })
            }
          }
        })
      },
    })
  },

  toAddressList: function () {
    wx.navigateTo({
      url: '../address/address?type=1',
    })
  },
 
  leaveMsg1: function (e) {
    this.setData({
      msg1: e.detail.value
    })
  },
  //商家配送去支付
  shangjia: function () {
    if (this.data.name == '') {
      wx.showToast({
        title: "请选择收货地址",
        image: '../../images/error.png',
      })
    } else {
      // var fei = 0;
      // var arr = new Array("福建", "内蒙", "广西", "海南", "甘肃", "宁夏", "青海", "云南", "贵州", "西藏", "新疆");
      // for (var i = 0; i < arr.length; i++) {
      //   if (arr[i] == str.substring(0, 2)) {
      //     if ("西藏" == str.substring(0, 2)) {
      //       fei = (total - num) * 500;
      //     } else if ("新疆" == str.substring(0, 2)) {
      //       fei = (total - num) * 500;
      //     } else if (memberType == "0") {
      //       fei = (total - num) * 5;
      //     }
      //   }
      // }

      wx.navigateTo({
        url: '../pay/pay?ids=' + this.data.ids + '&money=' + this.data.money + '&number=' + this.data.number + '&deliveryId=' + this.data.deliveryId + '&type=0' + '&msg=' + this.data.msg1 + '&state=' + this.data.tg_or_ms,
      })
    }

  },

})